package com.yundasys.lab04springbootlocalmsg.service.impl;

import com.yundasys.lab04springbootlocalmsg.dao.AccountADao;
import com.yundasys.lab04springbootlocalmsg.dao.PaymentMsgDao;
import com.yundasys.lab04springbootlocalmsg.entity.AccountA;
import com.yundasys.lab04springbootlocalmsg.entity.PaymentMsg;
import com.yundasys.lab04springbootlocalmsg.service.PaymentMsgService;
import com.yundasys.lab04springbootlocalmsg.service.PaymentService;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * (PaymentMsg)表服务实现类
 *
 * @author makejava
 * @since 2021-01-13 22:46:04
 */
@Service("paymentService")
public class PaymentServiceImpl implements PaymentService {

    @Resource
    private AccountADao accountADao;
    @Resource
    private PaymentMsgDao paymentMsgDao;
    @Autowired
    private DefaultMQProducer producer;

    /**
     * 支付接口
     * @param userId
     * @param orderId
     * @param amount
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int payment(int userId, int orderId, BigDecimal amount) {

        AccountA accountA = accountADao.queryById(userId);
        if(accountA == null) return 1;
        if(accountA.getBalance().compareTo(amount) <0) return 2;
        accountA.setBalance(accountA.getBalance().subtract(amount));
        accountADao.update(accountA);

        PaymentMsg paymentMsg = new PaymentMsg();
        paymentMsg.setOrderId(orderId);
        paymentMsg.setStatus(0);
        paymentMsg.setFailureCnt(0);
        paymentMsg.setCreateTime(new Date());
        paymentMsg.setCreateUser(userId);
        paymentMsg.setUpdateTime(new Date());
        paymentMsg.setUpdateUser(userId);
        paymentMsgDao.insert(paymentMsg);

        return 0;
    }


    /**
     * 支付接口
     * @param userId
     * @param orderId
     * @param amount
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int paymentMQ(int userId, int orderId, BigDecimal amount) throws Exception {

        AccountA accountA = accountADao.queryById(userId);
        if(accountA == null) return 1;
        if(accountA.getBalance().compareTo(amount) <0) return 2;
        accountA.setBalance(accountA.getBalance().subtract(amount));
        accountADao.update(accountA);

        Message message = new Message();
        message.setTopic("payment");
        message.setKeys(orderId+"");
        message.setBody("订单已支付".getBytes());
        try {
            SendResult send = producer.send(message);
            if(send.getSendStatus() == SendStatus.SEND_OK) {
                return 0;
            } else {
                throw new Exception("消息发送失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

}